package com.ijmacd.footballtracker;

import java.io.IOException;
import java.net.URI;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.ijmacd.footballtracker.Provider.Teams;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TeamListActivity extends ListActivity {
	
	static final String TAG = "TeamListActivity";

	private static final int MENU_ITEM_REFRESH = 0;
	
	final Handler uiThreadCallback = new Handler();

	public int addedTeams;

	private Cursor mCursor;
	
	/**
     * The columns we are interested in from the database
     */
    private static final String[] PROJECTION = new String[] {
            Provider.Teams._ID, // 0
            Provider.Teams.NAME, // 1
    };

	private static final int COLUMN_INDEX_UUID = 0;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		

        // If no data was given in the intent (because we were started
        // as a MAIN activity), then use our default content provider.
        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(Provider.Teams.CONTENT_URI);
        }

        // Perform a managed query. The Activity will handle closing and requerying the cursor
        // when needed.
        mCursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
                Provider.Teams.DEFAULT_SORT_ORDER);

        // Used to map notes entries from the database to views
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mCursor,
                new String[] { Provider.Teams.NAME }, new int[] { android.R.id.text1 });
        setListAdapter(adapter);
		
		getListView().setTextFilterEnabled(true);
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // This is our one standard application action -- inserting a
        // new note into the list.
        menu.add(0, MENU_ITEM_REFRESH, 0, R.string.menu_refresh)
                .setIcon(R.drawable.refresh);

        // Generate any additional actions that can be performed on the
        // overall list.  In a normal install, there are no additional
        // actions found here, but this allows other applications to extend
        // our menu with their own actions.
        Intent intent = new Intent(null, getIntent().getData());
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, Menu.NONE, Menu.NONE,
                new ComponentName(this, MatchListActivity.class), null, intent, 0, null);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_ITEM_REFRESH:
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
    	//Log.d(TAG, "List Item Clicked" + getListView().getItemAtPosition(position).getClass().toString());
    	mCursor.moveToPosition(position);
        Uri uri = Uri.parse(getIntent().getData().toString() + "/" + mCursor.getString(COLUMN_INDEX_UUID));
        
        String action = getIntent().getAction();
        if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
            // The caller is waiting for us to return a note selected by
            // the user.  The have clicked on one, so return it now.
            setResult(RESULT_OK, new Intent().setData(uri));
            finish();
        } else {
            // Launch activity to view/edit the currently selected item
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    }
}
